package com.xiaoshuidi.cloud.module.member.service.activities;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageParam;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.mybatis.core.util.MyBatisUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.activities.vo.ActivitiesCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.activities.vo.ActivitiesDetailVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.activities.vo.ActivityPageVO;
import com.xiaoshuidi.cloud.module.member.controller.app.activities.vo.ActivitiesListVO;
import com.xiaoshuidi.cloud.module.member.enums.ActivitiesStateEnum;
import com.xiaoshuidi.cloud.module.member.mapper.activities.MemberActivitiesMapper;
import com.xiaoshuidi.cloud.module.member.pojo.activities.MemberActivities;
import com.xiaoshuidi.cloud.module.member.repository.MemberActivitiesEnrollRepository;
import com.xiaoshuidi.cloud.module.member.repository.MemberActivitiesRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberActivitiesServiceImpl extends ServiceImpl<MemberActivitiesMapper, MemberActivities> implements MemberActivitiesService {

    private final MemberActivitiesRepository memberActivitiesRepository;

    private final MemberActivitiesEnrollRepository memberActivitiesEnrollRepository;

    private final MemberActivitiesEnrollService memberActivitiesEnrollService;

    @Override
    public Boolean create(ActivitiesCreateVO vo) {
        memberActivitiesRepository.create(vo);
        return true;
    }

    @Override
    public PageResult<ActivitiesListVO> getList(PageParam pageParam) {
        IPage<ActivitiesListVO> page = MyBatisUtils.buildPage(pageParam);
        List<MemberActivities> list = this.baseMapper.getList(page);
        if (list != null && list.size() > 0) {
            //查询当前活动报名人数
            //2.1 获取活动ids
            List<Long> ids = list.stream().map(MemberActivities::getId).collect(Collectors.toList());
            //获取对应活动报名人数map
            Map<Long, Integer> map = memberActivitiesEnrollRepository.getEnrollNum(ids);

            List<ActivitiesListVO> collect = list.stream().map(a -> {
                ActivitiesListVO vo = new ActivitiesListVO();
                BeanUtils.copyProperties(a, vo);
                vo.setFillItemList(JsonUtils.parseArray(a.getFillItem(), String.class));
                Integer num = map.get(a.getId());
                vo.setEnrollNum(num == null ? 0 : num);
                Integer activityStatus = memberActivitiesRepository.getActiviesStatus(a);
                vo.setActivityStatus(activityStatus);
                Boolean flag = memberActivitiesEnrollService.enrollOrNot(a.getId());
                vo.setEnrollOrNot(flag);
                return vo;
            }).collect(Collectors.toList());
            PageResult<ActivitiesListVO> pageResult = new PageResult<>(collect, page.getTotal());
            return pageResult;
        }
        PageResult<ActivitiesListVO> pageResult = PageResult.empty();
        return pageResult;
    }

    @Override
    public PageResult<ActivitiesListVO> getPage(ActivityPageVO pageVO) {
        LocalDateTime now = LocalDateTime.now();
        IPage<MemberActivities> page = memberActivitiesRepository.getActiveiesPage(pageVO, now);
        List<MemberActivities> records = page.getRecords();
        List<Long> ids = records.stream().map(MemberActivities::getId).collect(Collectors.toList());
        //查询活动参与人数
        Map<Long, Integer> map = new HashMap<>();
        if (ids != null && ids.size() > 0) {
            map = memberActivitiesEnrollRepository.getEnrollNum(ids);
        }
        final Map<Long, Integer> enrollMap = map;
        List<ActivitiesListVO> collect = records.stream().map(m -> {
            ActivitiesListVO activitiesListVO = new ActivitiesListVO();
            BeanUtils.copyProperties(m, activitiesListVO);
            if (ObjectUtils.isNotEmpty(pageVO.getStatus())) {
                activitiesListVO.setStatus(pageVO.getStatus());
            } else {
                activitiesListVO.setStatus(getActivitiesStatus(m, now));
            }

            Integer num = enrollMap.get(m.getId());
            activitiesListVO.setEnrollNum(num);
            if (StringUtils.isNotEmpty(m.getFillItem())) {
                activitiesListVO.setFillItemList(JsonUtils.parseArray(m.getFillItem(), String.class));
            }
            return activitiesListVO;
        }).collect(Collectors.toList());
        PageResult<ActivitiesListVO> pageResult = PageResult.empty();
        pageResult.setTotal(page.getTotal());
        pageResult.setList(collect);
        return pageResult;
    }

    @Override
    public ActivitiesDetailVO getById(Long id) {
        MemberActivities byId = memberActivitiesRepository.getById(id);
        ActivitiesDetailVO vo = new ActivitiesDetailVO();
        BeanUtils.copyProperties(byId, vo);
        if (StringUtils.isNotEmpty(byId.getFillItem())) {
            vo.setFillItemList(JsonUtils.parseArray(byId.getFillItem(), String.class));
        }
        return vo;
    }

    @Override
    public Boolean edit(ActivitiesCreateVO vo) {
        memberActivitiesRepository.edit(vo);
        return true;
    }

    private Integer getActivitiesStatus(MemberActivities a, LocalDateTime now) {
        //未生效 开始时间>当前时间
        if (a.getBeginTime().isAfter(now)) {
            return ActivitiesStateEnum.TO_BE_EFFECTIVE.getCode();
        } else if ((a.getEndTime().isAfter(now) && now.isAfter(a.getBeginTime())) || a.getBeginTime() == now) {
            //已生效 开始时间<=当前时间<结束时间
            return ActivitiesStateEnum.EFFECTIVE.getCode();
        } else {
            return ActivitiesStateEnum.EXPIRE.getCode();
        }
    }

}
